package io.openkunlun.examples.springboot.provider;

import com.google.common.collect.Lists;
import io.openkunlun.examples.springboot.api.RpcRequest;
import io.openkunlun.examples.springboot.api.RpcResponse;
import io.openkunlun.examples.springboot.api.RpcService;
import io.openkunlun.examples.springboot.api.RpcService2;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Configuration
public class RpcServiceImpl implements RpcService {

    private final static Logger logger = LoggerFactory.getLogger(RpcService.class);

    @Resource
    private RpcService2 rpcService2;

    @Override
    public RpcResponse rpc(RpcRequest request) {
        rpcService2.test();
        logger.info("----------------rpc---------------------");
        logger.info(String.format("方法rpc被调用, request:%s", request));
        return new RpcResponse("收到消息: " + request + ", 返回消息:" + System.currentTimeMillis());
    }

    @Override
    public RpcResponse list(List<RpcRequest> request) {
        logger.info("----------------list---------------------");
        logger.info(String.format("方法list被调用, request:%s", request));
        return new RpcResponse("收到列表数量: " + request.size() + ", 返回消息:" + System.currentTimeMillis());
    }

    @Override
    public void multi(String name, int age) {
        logger.info("----------------multi---------------------");
        logger.info(String.format("方法multi被调用, name:%s, age:%s", name, age));
    }

    @Override
    public List<RpcResponse> map(Map<String, List<RpcRequest>> request) {
        logger.info("----------------map---------------------");
        logger.info(String.format("方法map被调用, request size:%s", request.size()));
        return Lists.newArrayList(new RpcResponse("消息收到"));
    }

    @Override
    public void error() {
        logger.info("----------------error---------------------");
        throw new NullPointerException();
    }
}
